package cn.terminus.biz.customer.customdemand;

import cn.terminus.biz.customer.common.AbstractTerminusManager;
import cn.terminus.common.customer.bean.customdemand.dto.*;
import cn.terminus.data.customer.customdemand.dao.CustomDemandDao;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.util.BeanCopyUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * (CustomDemand)表服务实现类
 *
 * @author makejava
 * @since 2021-07-08 11:18:44
 */
@Component
public class CustomDemandManager extends AbstractTerminusManager {
    @Autowired
    private CustomDemandDao customDemandDao;

    public Integer add(CustomDemandNewDto dto) throws DataAccessException {
        try {
            return customDemandDao.add(dto);
        } catch (Exception e) {
            logger.error("CustomDemandManager新增失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer update(CustomDemandNewDto dto) throws DataAccessException {
        try {
            return customDemandDao.update(dto);
        } catch (Exception e) {
            logger.error("CustomDemandManager更新失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomDemandResultDto> gets(CustomDemandGetsDto dto) throws DataAccessException {
        try {
            return customDemandDao.gets(dto);
        } catch (Exception e) {
            logger.error("CustomDemandManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomDemandResultDto> query(CustomDemandQueryDto dto) throws DataAccessException {
        try {
            return customDemandDao.query(dto);
        } catch (Exception e) {
            logger.error("CustomDemandManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer delete(CustomDemandDeleteDto dto) throws DataAccessException {
        try {
            return customDemandDao.delete(dto);
        } catch (Exception e) {
            logger.error("CustomDemandManager删除失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public TerminusPageAble<CustomDemandResultDto> page(CustomDemandQueryDto dto, TerminusPageInfo page) throws DataAccessException {
        try {
            PageHelper.startPage(page.getCurrentPage(), page.getPageSize());
            Page<CustomDemandResultDto> result = (Page<CustomDemandResultDto>) customDemandDao.query(dto);
            return new TerminusPageAble<>(BeanCopyUtils.copy(result.getResult(), CustomDemandResultDto.class), page, result.getTotal());
        } catch (Exception e) {
            logger.error("CustomDemandManager分页查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }
}
